package com.hzmall.controller.backend;

import com.hzmall.common.ServerResponse;
import com.hzmall.pojo.Product;
import com.hzmall.service.impl.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping(value = "/manage/product/")
public class ProductBackendController {

    @Autowired
    private ProductService productService;

    @RequestMapping(value = "list.do")
    @ResponseBody
    public ServerResponse list(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,@RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){
        return this.productService.backendList(pageNum, pageSize);
    }

    //产品搜索
    @RequestMapping(value = "search.do")
    @ResponseBody
    public ServerResponse search(String productName,Integer productId,
                                 @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                 @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){
        return this.productService.backendSearch(productName, productId, pageNum, pageSize);
    }

    //图片上传
    @RequestMapping(value = "upload.do")
    @ResponseBody
    public ServerResponse upload(MultipartFile upload_file, HttpServletRequest request){
        return this.productService.upload(upload_file, request);
    }

    //商品详情
    @RequestMapping(value = "detail.do")
    @ResponseBody
    public ServerResponse detail(Integer productId){
        return this.productService.backendDetail(productId);
    }
    //更改产品状态
    @RequestMapping(value = "set_sale_status.do")
    @ResponseBody
    public ServerResponse setSaleStatus(Integer productId,Integer status){
        return this.productService.setSaleStatus(productId, status);
    }
    //新增or更新产品
    @RequestMapping(value = "save.do")
    @ResponseBody
    public ServerResponse save(Product product){
        return this.productService.save(product);
    }
    //富文本图片上传
    @RequestMapping(value = "richtext_img_upload.do")
    @ResponseBody
    public Map richTextImgUpload(MultipartFile upload_file,HttpServletRequest request){
        return this.productService.backendRichTextImgUpload(upload_file, request);
    }
}
